/*
 * Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.evenmo.string;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * This is Description java提供的原子操作类
 *
 * @author moyongjun
 * @date 2020/01/02
 */
public class ThreadDemo_AtomXXX {

    private AtomicInteger integer=new AtomicInteger();

    public void add(){
        for (int i = 0; i < 50; i++) {
            integer.incrementAndGet();//代替integer++
            System.out.println(integer.get());
        }


    }

    public static void main(String[] args) {
        ThreadDemo_AtomXXX ta=new ThreadDemo_AtomXXX();
        List<Thread> threads=new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            threads.add(new Thread(ta::add,"thread"+(i+1)));

        }

        threads.forEach(thread -> thread.start());

        threads.forEach(thread -> {
            try {
                thread.join();
             //   System.out.println(ta.integer.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });


       // int i = ta.integer.get();
       // System.out.println(i);
    }
}
